package com.elinkway.infinitemovies.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.view.PublicLoadLayout;

import java.lang.reflect.Field;

public class UIs {

	// 渲染layout
	public static View inflate(Context context, int resource, ViewGroup root,
			boolean attachToRoot) {
		return LayoutInflater.from(context).inflate(resource, root,
				attachToRoot);
	}

	public static View inflate(Context context, int resource, ViewGroup root) {
		return LayoutInflater.from(context).inflate(resource, root);
	}

	/**
	 * 根据资源ID得到View
	 * */
	public static View inflate(LayoutInflater inflater, int resource,
			ViewGroup root, boolean attachToRoot) {
		return inflater.inflate(resource, root, attachToRoot);
	}

	private static Toast mToast = null;

	public static void showToast(String text) {
		if (mToast != null) {
			mToast.cancel();
		}
		mToast = Toast.makeText(MoviesApplication.getInstance(), text,
				Toast.LENGTH_SHORT);
		mToast.setText(text);
		//mToast.setGravity(Gravity.CENTER, 0, 0);
		mToast.setDuration(Toast.LENGTH_SHORT);
		mToast.show();
	}

	public static void showToast(int txtId) {
		if (mToast != null) {
			mToast.cancel();
		}
		mToast = Toast.makeText(MoviesApplication.getInstance(), txtId,
				Toast.LENGTH_SHORT);
		mToast.setText(txtId);
		//mToast.setGravity(Gravity.CENTER, 0, 0);
		mToast.setDuration(Toast.LENGTH_SHORT);
		mToast.show();
	}

	public static void hideSoftkeyboard(Activity mActivity) {
		if (null != mActivity && null != mActivity.getCurrentFocus()) {
			InputMethodManager mInputMethodManager = (InputMethodManager) mActivity
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (null != mInputMethodManager) {
				mInputMethodManager.hideSoftInputFromWindow(mActivity
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	public static double getDensity() {
		return MoviesApplication.getInstance().getResources()
				.getDisplayMetrics().density;
	}
	public static int dipToPx(int dipValue) {
		final float scale = MoviesApplication.getInstance().getResources()
				.getDisplayMetrics().density;
		int pxValue = (int) (dipValue * scale + 0.5f);

		return pxValue;
	}

	public static float dipToPxFloat(int dipValue) {
		final float scale = MoviesApplication.getInstance().getResources()
				.getDisplayMetrics().density;
		float pxValue = dipValue * scale;

		return pxValue;
	}

	public static int getScreenWidth() {
		return ((WindowManager) MoviesApplication.getInstance()
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
				.getWidth();
	}

	public static int getScreenHeight() {
		DisplayMetrics dm = new DisplayMetrics();
		((WindowManager) MoviesApplication.getInstance()
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
				.getMetrics(dm);
		return dm.heightPixels;
	}

	public static boolean hasNet() {
		NetworkInfo networkInfo = NetWorkTypeUtils.getAvailableNetWorkInfo();

		if (networkInfo == null) {
			return false;
		}
		return true;
	}

	public static PublicLoadLayout createPage(Context context, int layoutId) {
		PublicLoadLayout rootView = new PublicLoadLayout(context);
		rootView.addContent(layoutId);
		return rootView;
	}

	public static int getBarHeight(Context context){
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0, sbar = 38;//默认为38，貌似大部分是这样的

		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			sbar = context.getResources().getDimensionPixelSize(x);

		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return sbar;
	}

	public static void showOrHideBg(View v, boolean isShow) {
		if(isShow){
			v.setVisibility(View.VISIBLE);
			//初始化
			Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
			alphaAnimation.setDuration(300);
			v.startAnimation(alphaAnimation);
		}else{
			if(v.getVisibility() == View.VISIBLE){
				Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
				alphaAnimation.setDuration(300);
				v.startAnimation(alphaAnimation);
			}
			v.setVisibility(View.GONE);
		}
	}

	/**
	 * 返回当前屏幕是否为竖屏。
	 *
	 * @param context
	 * @return 当且仅当当前屏幕为竖屏时返回true,否则返回false。
	 */
	public static boolean isScreenOriatationPortrait(Context context) {
		return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
	}

	public static boolean isOnScreen(Activity context, View view, String index){
		boolean isOnScreen = false;
		int[] location = new int[2];
		WindowManager windowManager = context.getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		int screenWidth = display.getWidth();
		int screenHeight = display.getHeight();
		view.getLocationOnScreen(location);
		if(location[0] >=0 && location[0] <screenWidth && location[1]>0 && location[1]<screenHeight){
            isOnScreen = true;
		}
		return isOnScreen;
	}

	public static boolean isOnScreenTop(View view) {
		int[] locations = new int[2];
		view.getLocationOnScreen(locations);
		return locations[1] == 0;
	}

	public static int getStatusBarHeight() {
		int result = 0;
		Resources resources = MoviesApplication.getInstance().getResources();
		int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
		if (resourceId > 0) {
			result = resources.getDimensionPixelSize(resourceId);
		}
		return result;
	}
}
